package com.xaicode.template.jaeger;

import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cjxia@isoftstone.com
 * @date 2022/5/9 0009 10:10
 */
@RestController
@RequestMapping("jaeger")
public class TestController {

    @Resource
    private JaegerUtil jaegerUtil;

    @GetMapping("t1")
    public String t1(String span, String msg) {

        JaegerUtil.Tag tag1 = jaegerUtil.buildTag("name", "n" + RandomUtil.randomNumbers(1));
        JaegerUtil.Tag tag2 = jaegerUtil.buildTag("ope", "t1");


        msg = RandomUtil.randomString(5);

        JaegerUtil.Msg jMsg = jaegerUtil.buildMsg(span, msg, tag1, tag2);
        jaegerUtil.add(jMsg);

        return HttpStatus.OK.name();
    }

    @GetMapping("t2")
    public String t2(String span, String msg, String tag) {

        String[] tagForms = tag.split(",");
        List<JaegerUtil.Tag> tags = Lists.newArrayList();
        for (int i = 0; i < tagForms.length; i = i + 2) {
            JaegerUtil.Tag newTag = jaegerUtil.buildTag(tagForms[i], tagForms[i + 1]);
            tags.add(newTag);
        }

        jaegerUtil.send(span, msg, tags.toArray(new JaegerUtil.Tag[]{}));

        return HttpStatus.OK.name();
    }

}
